package com.yp.infrastructure.shiro.model;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;



@Slf4j
@Component
public class CustomRealm extends AuthorizingRealm implements BeanPostProcessor {

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
            throws AuthenticationException {
        log.debug("doGetAuthenticationInfo");
        Object credentials = authenticationToken.getCredentials();
        if (credentials == null) {
            throw new AuthenticationException("权限验证失败");
        }
        String token = (String) credentials;
        log.debug("doGetAuthenticationInfo:{}", token);
        return new SimpleAuthenticationInfo(token, token, "userRealm");
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        log.debug("doGetAuthenticationInfo:{}", principals);
        return new SimpleAuthorizationInfo();
    }

}
